package com.xiaoxi.chainofreponsibilitymode.support;

import com.xiaoxi.chainofreponsibilitymode.trouble.Trouble;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.chainofreponsibilitymode.support
 * @ClassName Support
 * @Description TODO
 * @Date 2023/3/23 20:22
 * @Version 1.0
 */
public abstract class Support {

    protected String name;
    private Support next;


    public Support(String name) {
        this.name = name;
    }

    public Support setSupport(Support support) {
        this.next = support;
        return next;
    }

    public void support(Trouble trouble) {
        if (resolve(trouble)) {
            done(trouble);
        } else if (next != null) {
            next.support(trouble);
        } else {
            fail(trouble);
        }
    }

    protected abstract boolean resolve(Trouble trouble);

    private void done(Trouble trouble) {
        System.out.println(name + " 已解决 " + trouble.getName());
    }

    private void fail(Trouble trouble) {
        System.out.println(name + " 未解决 " + trouble.getName());
    }


}
